package org.irri.smta.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.irri.smta.domain.AccessPrivilege;
import org.irri.smta.domain.Role;
import org.irri.smta.repository.IAccessPrivilegeRepository;
import org.irri.smta.service.IAccessPrivilegeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

@Named("accessPrivilegeService")
@Transactional(readOnly = true)
public class AccessPrivilegeService implements IAccessPrivilegeService<AccessPrivilege, Long> {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(AccessPrivilegeService.class);

	@Inject
	@Named("accessPrivilegeRepository")
	private IAccessPrivilegeRepository accessPrivilegeRepository;
	
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	@Override
	public AccessPrivilege create(AccessPrivilege accessPrivilege) {
		LOGGER.debug("AccessPrivilege create(AccessPrivilege accessPrivilege): " + accessPrivilege);
		Date now = new Date();
		accessPrivilege.setCreatedDate(now);
		accessPrivilege.setUpdatedDate(now);
		LOGGER.debug("Creating a new accessPrivilege with information: " + accessPrivilege);
		return accessPrivilegeRepository.save(accessPrivilege);
	}

	@Transactional(readOnly = false, rollbackFor = Exception.class)
	@Override
	public AccessPrivilege update(AccessPrivilege accessPrivilege) throws Exception {
		LOGGER.debug("AccessPrivilege update(AccessPrivilege accessPrivilege): " + accessPrivilege);
		accessPrivilege.setUpdatedDate(new Date());
		LOGGER.debug("Updating accessPrivilege with information: " + accessPrivilege);

		AccessPrivilege accessPrivilegeDb = accessPrivilegeRepository.findOne(accessPrivilege.getId());

		if (accessPrivilegeDb == null) {
			LOGGER.debug("No accessPrivilege found with id: " + accessPrivilege.getId());
			throw new Exception();
		}

		accessPrivilegeRepository.save(accessPrivilege);
		return accessPrivilege;
	}

	@Transactional(readOnly = false, rollbackFor = Exception.class)
	@Override
	public AccessPrivilege delete(Long id) throws Exception {
		LOGGER.debug("AccessPrivilege delete(Long id): " + id);

		AccessPrivilege accessPrivilege = accessPrivilegeRepository.findOne(id);

		if (accessPrivilege == null) {
			LOGGER.debug("No accessPrivilege found with id: " + id);
			throw new Exception();
		}

		accessPrivilegeRepository.delete(accessPrivilege);
		return accessPrivilege;
	}

	@Transactional(readOnly = true)
	@Override
	public AccessPrivilege findById(Long id) {
		LOGGER.debug("AccessPrivilege findById(Long id): " + id);
		return accessPrivilegeRepository.findOne(id);
	}

	@Transactional(readOnly = true)
	@Override
	public List<AccessPrivilege> findAll() {
		LOGGER.debug("List<AccessPrivilege> findAll()");
		return accessPrivilegeRepository.findAll();
	}

	@Transactional(readOnly = true)
	@Override
	public List<AccessPrivilege> findAllOrdered() {
		LOGGER.debug("findAll()");
		return accessPrivilegeRepository.findAllOrdered();
	}
	
	@Transactional(readOnly = true)
	@Override
	public List<AccessPrivilege> getAccessPrivilegesForRole(Role role) {
		LOGGER.debug("List<AccessPrivilege> getAccessPrivilegesForRole(Role role): " + role);
		List<AccessPrivilege> result = new ArrayList<AccessPrivilege>();
		for (AccessPrivilege ap : findAll()) {
			if (ap.getRole().getId().longValue() == role.getId().longValue()) {
				result.add(ap);
			}
		}
		return result;
	}

}
